package com.yonyou.distribute.lock;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;

import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class DistributedRedisLock implements Lock {

    private String lockName;
    private StringRedisTemplate stringRedisTemplate;
    private String uuid;
    private long expire = 30;

    public DistributedRedisLock(String lockName, StringRedisTemplate stringRedisTemplate, String uuid) {
        this.lockName = lockName;
        this.stringRedisTemplate = stringRedisTemplate;
        this.uuid = uuid + ":" + Thread.currentThread().getId();
    }

    @Override
    public void lock() {
        this.tryLock();
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {

    }

    @Override
    public boolean tryLock() {
        try {
            return this.tryLock(-1L, TimeUnit.SECONDS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 加锁方法
     * @param time
     * @param unit
     * @return
     * @throws InterruptedException
     */
    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        if(time != -1){
            expire = unit.toSeconds(time);
        }
        String script = "if redis.call('exists', KEYS[1]) == 0 or redis.call('hexists', KEYS[1], ARGV[1]) == 1 " +
                "then " +
                    "redis.call('hincrby', KEYS[1], ARGV[1], 1) " +
                    "redis.call('expire', KEYS[1], ARGV[2]) " +
                    "return 1 " +
                "else " +
                    "return 0 " +
                "end";
        while(!stringRedisTemplate.execute(new DefaultRedisScript<>(script, Boolean.class), Arrays.asList(lockName), uuid, String.valueOf(expire))){
            TimeUnit.SECONDS.sleep(50);
        }
        // 加锁成功，返回之前，开启定时器自动续期
        this.reNewExpire();
        return true;
    }

    @Override
    public void unlock() {
        String script = "if redis.call('hexists', KEYS[1], ARGV[1]) == 0 " +
                "then " +
                    "return nil " +
                "elseif redis.call('hincrby',  KEYS[1], ARGV[1], -1) == 0 " +
                "then " +
                    "return redis.call('del', KEYS[1]) " +
                "else " +
                    "return 0 " +
                "end";
        //nil 对应Long中null
        Long flag = stringRedisTemplate.execute(new DefaultRedisScript<>(script, Long.class), Arrays.asList(lockName), uuid);
        if(flag == null){
            throw new IllegalMonitorStateException("this lock does not belong you!");
        }
    }

    @Override
    public Condition newCondition() {
        return null;
    }



    /**
     * 自动续期，相当于开启了一个子线程
     */
    private void reNewExpire(){
        String script = "if redis.call('hexists', KEYS[1], ARGV[1]) == 1 " +
                        "then " +
                              "return redis.call('expire', KEYS[1], ARGV[2]) " +
                        "else " +
                              "return 0 " +
                        "end";
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                if(stringRedisTemplate.execute(new DefaultRedisScript<>(script, Boolean.class), Arrays.asList(lockName), uuid, String.valueOf(expire))){
                    reNewExpire();
                }
            }
        }, expire * 1000 / 3);
    }
}
